package tree

import (
	"learn-opengl/camera"
	"learn-opengl/object/block"
	"learn-opengl/opengl/shader"
	. "learn-opengl/util"
	"math"

	"github.com/go-gl/mathgl/mgl32"
)



type Tree struct {
	Data map[IV3]int
}

func NewTree(pos IV3) *Tree {
	tree := &Tree{}
	tree.Data = make(map[IV3]int)
	for i:=0;i<len(treeData);i+=4 {
		tree.Data[IV3{
			pos[0] + treeData[i],pos[1] + treeData[i+1],pos[2] + treeData[i+2],
		}] = treeData[i+3]
	}
	return tree
}

func (t *Tree) Draw(camera *camera.Camera,shader *shader.Shader) {
	for item,val := range t.Data {
		tem := mgl32.Vec3{float32(item[0]),float32(item[1]),float32(item[2])}
		cPos := camera.GetPos()
		front := camera.GetFront()
		p := tem.Sub(cPos)
		angle := math.Acos(float64(front.Normalize().Dot(p.Normalize())))
		if p.Len()>50 && tem[1] < 10 {
			continue
		}
		if angle < math.Pi/2  {
			block.Blocks[val].Draw(shader,tem)
		}
	}
}

// 5 15
var treeData = []int {
	0,0,0,5,
	0,1,0,5,
	0,2,0,5,
	0,3,0,5,

	1,2,1,15,
	1,2,-1,15,
	1,2,0,15,
	-1,2,1,15,
	-1,2,-1,15,
	-1,2,0,15,
	0,2,1,15,
	0,2,-1,15,

	2,2,0,15,
	-2,2,0,15,
	0,2,2,15,
	0,2,-2,15,

	1,3,1,15,
	1,3,-1,15,
	1,3,0,15,
	-1,3,1,15,
	-1,3,-1,15,
	-1,3,0,15,
	0,3,1,15,
	0,3,-1,15,

	0,4,0,15,
}
